/**
 * @author mili
 */

var debug = false;
var maxTimestamp = 10;
var map, incidentData, heatmap;
var markers, incidentWindows;
var cluster = true;
var time = false;

function switchMode(){
	if(debug === false){
		debug = true;
		document.getElementById("switchButton").value="debug on";
		alert("You turn off the debug mode on");
	}else{
		debug = false;
		document.getElementById("switchButton").value="debug off";
		alert("Your turn on the debug mode off");
	}
}

/*
Call to switch category 2 time flag
*/
function switchTimeFlag(){
	time = !time;
	initialize();
	changeRadius();
	changeGradient();
}

function switchCluster(){
	cluster = true;
	initialize();
	changeRadius();
	changeGradient();
}

function switchHeatMap(){
	cluster = false;
	initialize();
	changeRadius();
	changeGradient();
}

function init(){
	getTimeStamp();
	initialize();
	changeRadius();
	changeGradient();
}


function initialize(){
	
	var mapOptions = {
          zoom: 10,
          center: new google.maps.LatLng( -33.8674869, 151.2069902),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
      	setMap();
}

function setMap(){

	if(cluster){
      		markers = [];
      		incidentWindows = [];
      		var marker;
      		// marker image
			var image = new google.maps.MarkerImage('client_program/resources/error.png',
            // This marker is 20 pixels wide by 20 pixels tall.
            new google.maps.Size(24, 24),
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            new google.maps.Point(0, 32));
            if(incidents == undefined){
            	return;
            }
        	for(var i = 0; i < incidents.length; i++){
   		     	var infowindow = new google.maps.InfoWindow({content: getPropertyInfo_Cat1(features[i].properties)});
        		marker = new google.maps.Marker({
        			position: incidents[i],
        			map: map,
        			icon: image,
        			title:"incident",
        			incidentIndex: i
        		});
        		google.maps.event.addListener(marker, 'click', function() {incidentWindows[this.incidentIndex].open(map,this)});
        		incidentWindows.push(infowindow);
        		markers.push(marker);
        	}
        	var markerCluster = new MarkerClusterer(map, markers);
        	if(time){
				set_Category2_Markers(map,f3pos,f3info, "F3");
				set_Category2_Markers(map,m4pos,m4info, "M4");
				set_Category2_Markers(map,m7pos,m7info, "M7");
			}
    }else{
       		incidentData = new google.maps.MVCArray(incidents);

        	heatmap = new google.maps.visualization.HeatmapLayer({
        		data: incidentData
        	});

        	heatmap.setMap(map);
	}
}

function set_Category2_Markers(map, locations, descriptions, title){

	// marker image
	var image = new google.maps.MarkerImage('client_program/resources/time_machine.png',
            // This marker is 20 pixels wide by 32 pixels tall.
            new google.maps.Size(20, 20),
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            new google.maps.Point(0, 32));
            
            
    
    
    // shadow
    var shadow = new google.maps.MarkerImage('client_program/resources/time_machine.png',
            // The shadow image is larger in the horizontal dimension
            // while the position and offset are the same as for the main image.
            new google.maps.Size(37, 32),
            new google.maps.Point(0,0),
            new google.maps.Point(0, 32));
            // Shapes define the clickable region of the icon.
            // The type defines an HTML &lt;area&gt; element 'poly' which
            // traces out a polygon as a series of X,Y points. The final
            // coordinate closes the poly by connecting to the first
            // coordinate.
    
    
    // shape
    var shape = {
            coord: [1, 1, 1, 20, 18, 20, 18 , 1],
            type: 'poly'
    };
    
    var markers = [];
    var infowindows = [];
    
    for(var i = 0; i < locations.length; i++){    
   		var infowindow = new google.maps.InfoWindow({content: title+"<br>"+descriptions[i]});
    	var marker = new google.maps.Marker({
              position: locations[i],
              map: map,
              shadow: shadow,
              icon: image,
              shape: shape,
              title: "",
              zIndex: 0,
              infoWindowIndex: i,
              animation: google.maps.Animation.DROP
        });
        google.maps.event.addListener(marker, 'click', function() {infowindows[this.infoWindowIndex].open(map,this)});
    	infowindows.push(infowindow);
    	markers.push(marker);
    }
}

function toogleHeatMap(){
	heatmap.setMap(heatmap.getMap() ? null : map);
}

function changeGradient() {
	if(cluster)
		return;
		
        var gradient = [
          'rgba(0, 255, 255, 0)',
          'rgba(0, 255, 255, 1)',
          'rgba(0, 191, 255, 1)',
          'rgba(0, 127, 255, 1)',
          'rgba(0, 63, 255, 1)',
          'rgba(0, 0, 255, 1)',
          'rgba(0, 0, 223, 1)',
          'rgba(0, 0, 191, 1)',
          'rgba(0, 0, 159, 1)',
          'rgba(0, 0, 127, 1)',
          'rgba(63, 0, 91, 1)',
          'rgba(127, 0, 63, 1)',
          'rgba(191, 0, 31, 1)',
          'rgba(255, 0, 0, 1)'
        ]
        heatmap.setOptions({
          gradient: heatmap.get('gradient') ? null : gradient
        });
      }

function changeRadius() {
	if(cluster)
		return;
		
        heatmap.setOptions({radius: heatmap.get('radius') ? null : 20});
      }

function changeOpacity() {
	if(cluster)
		return;
		
        heatmap.setOptions({opacity: heatmap.get('opacity') ? null : 0.2});
      }




